#include <iostream>
#include "ZDataBase.h"
#include "ZDataFrame.h"



/*
void test_re_find()
{
	const std::wstring text = L"aa1jisBeijing#:ADMAMS_Plaintiff_FJIENF#trading_asvBeijing#:ADMAMSKOK_Plaintiff_FJIENEF#trading";
	const std::wregex claimant11(LR"(Beijing[#:;_]{0,2}([A-Z#.]{5,10})[_#]{0,2}Plaintiff_([A-Z#.]{5,15})#trading)");
	std::wsmatch matche_one;
	if (RE::re_find_one(claimant11, text, matche_one) && !(matche_one.empty()))
	{
		std::wcout << L"\nmatche_one: " << matche_one[1].str() << " ";
		std::wcout << L"\nmatche_one: " << matche_one[2].str() << " ";
	}
	std::cout << "\nxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
	std::vector<std::vector<std::wstring>> matche_results;
	RE::re_find_all(claimant11, text, matche_results);
	if (!matche_results.empty()) {
		for (const auto& match_group : matche_results) {
			for (const auto& match : match_group) {
				std::wcout << L"match_group_ " << match << std::endl;
			}
		}
	}

    std::wstring a = L"2019/3-01";
    std::wstring b = L"2015-02-03";
    std::wstring c = L"2015-2-3";
    std::wstring d = L"2015/2/3";
    std::wstring e = L"2015/02-03";
	std::wcout << RE::format_date(a) << std::endl;
	std::wcout << RE::format_date(b) << std::endl;
	std::wcout << RE::format_date(c) << std::endl;
	std::wcout << RE::format_date(d) << std::endl;
	std::wcout << RE::format_date(e) << std::endl;

}
*/




void test_dataframe_build()
{
	// 测试参数化构造函数
	using namespace NDFrame;
	ZDataFrame<std::wstring> df_raw{
			{0, 434, 74, 1038, 113, 604, 39, L"江苏省医疗门诊投费票据(电子)", L""},
			{1, 718, 121, 734, 138, 16, 17, L"C", L""},
			{2, 95, 193, 185, 213, 90, 20, L"票据代码:", L""},
			{3, 197, 193, 282, 211, 85, 18, L"32060119", L""},
			{4, 954, 193, 1044, 213, 90, 20, L"票据号码:", L""},
			{5, 1056, 193, 1161, 211, 105, 18, L"0001084072", L""},
			{6, 97, 221, 331, 240, 234, 19, L"交款人统一社会信用代码:", L""},
			{7, 343, 221, 532, 238, 189, 17, L"**************4318", L""},
			{8, 953, 221, 1023, 240, 70, 19, L"校验码:", L""},
			{9, 1034, 221, 1099, 238, 65, 17, L"998217", L""},
			{10, 97, 246, 166, 266, 69, 20, L"交款人:", L""},
			{11, 177, 244, 240, 266, 63, 22, L"吴睿桐", L""},
			{12, 954, 246, 1044, 266, 90, 20, L"开票日期:", L""},
			{13, 1056, 246, 1161, 264, 105, 18, L"2019-09-09", L""},
			{14, 113, 281, 197, 301, 84, 20, L"项目编码", L""},
			{15, 364, 281, 448, 299, 84, 18, L"项目名称", L""},
			{16, 637, 281, 678, 301, 41, 20, L"单位", L""},
			{17, 756, 281, 797, 301, 41, 20, L"数量", L""},
			{18, 873, 281, 916, 301, 43, 20, L"标准", L""},
			{19, 1013, 281, 1054, 301, 41, 20, L"金额", L""},
			{20, 1067, 281, 1104, 301, 37, 20, L"(元)", L""},
			{21, 1266, 281, 1307, 301, 41, 20, L"备注", L""},
			{22, 97, 307, 201, 322, 104, 15, L"AE04000010", L""},
			{23, 218, 305, 434, 324, 216, 19, L"(J)布洛芬混悬液(美林)", L""},
			{24, 600, 305, 701, 324, 101, 19, L"100ml:2g*1", L""},
			{25, 804, 307, 836, 322, 32, 15, L"1.0", L""},
			{26, 902, 307, 951, 322, 49, 15, L"12.91", L""},
			{27, 1124, 307, 1173, 322, 49, 15, L"12.91", L""},
			{28, 1270, 307, 1303, 324, 33, 17, L"0.0", L""},
			{29, 598, 328, 651, 348, 53, 20, L"瓶/瓶", L""},
			{30, 97, 352, 201, 369, 104, 17, L"AZ01010201", L""},
			{31, 218, 350, 536, 371, 318, 21, L"(J)0.9%氯化钠注射液(百特)[软袋]", L""},
			{32, 598, 350, 701, 371, 103, 21, L"250ml*1袋/", L""},
			{33, 804, 352, 836, 369, 32, 17, L"1.0", L""},
			{34, 912, 352, 953, 369, 41, 17, L"5.65", L""},
			{35, 1132, 352, 1175, 369, 43, 17, L"5.65", L""},
			{36, 1270, 352, 1303, 369, 33, 17, L"0.0", L""},
			{37, 598, 373, 619, 395, 21, 22, L"袋", L""},
			{38, 97, 398, 201, 414, 104, 16, L"AE02030006", L""},
			{39, 218, 396, 506, 416, 288, 20, L"(J)注射用头孢呋辛钠(明可欣)", L""},
			{40, 598, 396, 703, 416, 105, 20, L"0.75g*1瓶/", L""},
			{41, 803, 398, 836, 416, 33, 18, L"3.0", L""},
			{42, 912, 398, 953, 414, 41, 16, L"20.9", L""},
			{43, 1122, 398, 1175, 414, 53, 16, L"62.70", L""},
			{44, 1270, 398, 1303, 414, 33, 16, L"0.0", L""},
			{45, 598, 420, 619, 439, 21, 19, L"瓶", L""},
			{46, 97, 443, 201, 461, 104, 18, L"AX02030728", L""},
			{47, 216, 441, 475, 463, 259, 22, L"※(L)帕拉米韦氯化钠注射液", L""},
			{48, 598, 443, 709, 463, 111, 20, L"0.15g:0.9g:", L""},
			{49, 803, 443, 836, 461, 33, 18, L"2.0", L""},
			{50, 921, 443, 954, 461, 33, 18, L"138", L""},
			{51, 1110, 443, 1175, 461, 65, 18, L"276.00", L""},
			{52, 1270, 443, 1303, 461, 33, 18, L"0.5", L""},
			{53, 600, 467, 701, 486, 101, 19, L"100ml*1瓶/", L""},
			{54, 97, 490, 201, 508, 104, 18, L"AX020C0340", L""},
			{55, 218, 488, 495, 508, 277, 20, L"(J)0.9%氯化钠注射液[塑料瓶]", L""},
			{56, 600, 490, 692, 508, 92, 18, L"10ml:0.09", L""},
			{57, 804, 490, 836, 508, 32, 18, L"1.0", L""},
			{58, 912, 490, 954, 508, 42, 18, L"1.12", L""},
			{59, 1132, 490, 1175, 508, 43, 18, L"1.12", L""},
			{60, 1270, 490, 1303, 508, 33, 18, L"0.0", L""},
			{61, 598, 512, 682, 531, 84, 19, L"g*1支/支", L""},
			{62, 97, 535, 201, 553, 104, 18, L"AE02030103", L""},
			{63, 218, 533, 392, 555, 174, 22, L"(J)注射用青霉素钠", L""},
			{64, 598, 535, 701, 555, 103, 20, L"80万u*1瓶/", L""},
			{65, 803, 537, 836, 553, 33, 16, L"1.0", L""},
			{66, 921, 535, 954, 553, 33, 18, L"0.4", L""},
			{67, 1132, 535, 1175, 553, 43, 18, L"0.40", L""},
			{68, 1270, 535, 1303, 553, 33, 18, L"0.0", L""},
			{69, 598, 557, 619, 576, 21, 19, L"瓶", L""},
			{70, 95, 580, 181, 598, 86, 18, L"Q130100X", L""},
			{71, 216, 578, 298, 600, 82, 22, L"肌肉注射", L""},
			{72, 803, 580, 836, 600, 33, 20, L"1.0", L""},
			{73, 941, 580, 954, 600, 13, 20, L"4", L""},
			{74, 1132, 580, 1175, 598, 43, 18, L"4.00", L""},
			{75, 1270, 580, 1303, 600, 33, 20, L"0.0", L""},
			{76, 97, 619, 251, 639, 154, 20, L"金额合计(大写)", L""},
			{77, 884, 619, 956, 639, 72, 20, L"(小写)￥", L""},
			{78, 238, 684, 355, 705, 117, 21, L"业务流水号：", L""},
			{79, 360, 686, 456, 703, 96, 17, L"212008607", L""},
			{80, 631, 684, 764, 705, 133, 21, L"医疗机构类型:", L""},
			{81, 1025, 684, 1116, 703, 91, 19, L"医保类型:", L""},
			{82, 236, 707, 286, 727, 50, 20, L"性别：", L""},
			{83, 631, 707, 723, 729, 92, 22, L"就诊卡号：", L""},
			{84, 1025, 707, 1157, 729, 132, 22, L"社会保障号码：", L""},
			{85, 238, 730, 452, 750, 214, 20, L"基本医疗保险基金支付:", L""},
			{86, 238, 752, 473, 773, 235, 21, L"医保政策范围内个人自付:", L""},
			{87, 631, 752, 867, 773, 236, 21, L"医保政策范围外个人自费:", L""},
			{88, 238, 775, 411, 795, 173, 20, L"医保个人账户支付:", L""},
			{89, 631, 775, 764, 795, 133, 20, L"个人支付金额:", L""},
			{90, 1025, 775, 1118, 795, 93, 20, L"支付方式:", L""},
			{91, 156, 797, 202, 843, 46, 46, L"南京", L""},
			{92, 218, 799, 234, 818, 16, 19, L"记", L""},
			{93, 319, 799, 411, 818, 92, 19, L"账户余额:", L""},
			{94, 95, 885, 152, 906, 57, 21, L"收款单", L""},
			{95, 645, 885, 713, 904, 68, 19, L"复核人:", L""},
			{96, 919, 885, 990, 904, 71, 19, L"收款人:", L""},
			{97, 999, 885, 1064, 902, 65, 17, L"816101", L""},
			{98, 729, 908, 764, 926, 35, 18, L"2-1", L""}
		};

	format_print(df_raw);


	puts("==========================================\n");
	std::vector<std::reference_wrapper<int>> x0_list_ref = df_raw.values<Field::X0, int>();
	std::vector<std::reference_wrapper<int>> x1_list_ref = df_raw.values<Field::X1, int>();
	std::vector<std::reference_wrapper<int>> y0_list_ref = df_raw.values<Field::Y0, int>();
	std::vector<std::reference_wrapper<int>> y1_list_ref = df_raw.values<Field::Y1, int>();
	std::vector<std::reference_wrapper<int>> w1_list_ref = df_raw.values<Field::WIDTH, int>();
	std::vector<std::reference_wrapper<int>> h1_list_ref = df_raw.values<Field::HEIGHT, int>();

	PRINT_REF_VEC(x0_list_ref);
	PRINT_REF_VEC(x1_list_ref);
	PRINT_REF_VEC(y0_list_ref);
	PRINT_REF_VEC(y1_list_ref);
	puts("==========================================\n");



	puts("=================  访问单行 =========================\n");
	auto [idx, x0, y0, x1, y1, width, height, text, text_sep] = df_raw[0].values();
	std::wcout << L"idx: " << idx << L", x0: " << x0 << L", y0: " << y0 << L", x1: " << x1 << L", y1: " << y1
		<< L", height: " << height << L", text: " << text << L", text_sep: " << text_sep << std::endl;
	puts("==========================================\n");

    puts("=================  访问单元值 =========================\n");
    int cell_x0 = df_raw[0].ValueField<Field::X0>();
    int cell_y0 = df_raw[0].ValueField<Field::Y0>();
    std::wcout << L"cell_x0: " << cell_x0 << L", cell_y0: " << cell_y0 << std::endl;
    puts("==========================================\n");

	///////////////////////////////////////////////////////////////////////////////////////////
	auto df_filtered = df_raw.DF_filter([](const DataItem<std::wstring>& item) { return item.y1 < 279; });
	std::wcout << L"df_filtered: \n";
	format_print(df_filtered);


	///////////////////////////////////////////////////////////////////////////////////////////
	using VecItemPtr = std::vector<DataItem<std::wstring>*>;
	VecItemPtr pointers_xyxy;
	std::wregex regex_xyxy(LR"(.*(\d{3}ml).*)");

	// use generate_filtere
	const std::wregex regex_(LR"(.*(2{5}).*)");
	auto generate_filtered = df_filtered.DF_loc_regex_xyxy<Field::TEXT>(regex_, 124, 124, 540, 480);
	auto generate_filtered2 = df_filtered.DF_loc_xyxy(124, 124, 530, 470);
	std::cout << "\n\ngenerate_filtered(2 df): \n";
	format_print(generate_filtered);
	format_print(generate_filtered2);
}


//void test_filter_view(NDFrame::ZDataFrame& paramFrame)
//{
//	using namespace NDFrame;
//
//	ZDataView view(paramFrame);
//	std::cout << "\ntest_filter_view\n";
//	format_print(view);
//
//	ZDataView view2(paramFrame, 2, 5);
//	std::cout << "\ntest_filter_view2\n";
//	format_print(view2);
//}


//void test_sort_values(NDFrame::ZDataFrame<std::wstring>& drop_after_df)
//{
//	drop_after_df.sort_values(NDFrame::Field::X1);
//	std::cout << "\nsort X1: \n";
//	format_print(drop_after_df);
//
//	drop_after_df.sort_values(NDFrame::Field::HEIGHT, true);
//	std::cout << "\n descending sort HEIGHT: \n";
//	format_print(drop_after_df);
//}


int main() {
#ifndef _WIN32
	setlocale(LC_ALL, "zh_CN.UTF-8");
#endif // !_WIN32



	//测试正则
//	TIME_FUNCTION(test_re_find());

	// 测试各种构造函数, 各种筛选函数
	test_dataframe_build();

	////测试视图输出函数
	////std::cout << "=============================\n";
	//TIME_FUNCTION(test_filter_view(paramFrame));
	////
	////// 测试排序函数
	////std::cout << "============================\n";
	//TIME_FUNCTION(test_sort_values(paramFrame));
	////
	////std::cout << "============================\n";
	//format_print(paramFrame);
	return 0;
}



